package com.winbomb.kingcard.base;

import com.winbomb.kingcard.GameMsg;
import com.winbomb.kingcard.IGameRecord;

/**
 * 游戏信息记录器
 * 
 * @author winbomb
 * 
 */

public class GameRecorder implements IGameRecord {

	/** 最大的游戏信息记录条数 */
	public static final int MAX_RECORD_COUNT = 1000;

	/** 存放游戏信息记录的数组 */
	private GameMsg[] recordMsgs;

	/** 当前位置 */
	private int currPos;

	public GameRecorder() {
		this.recordMsgs = new GameMsg[MAX_RECORD_COUNT];
	}

	@Override
	public void add(GameMsg msg) {
		recordMsgs[currPos++] = msg;
		if (currPos >= MAX_RECORD_COUNT) {
			currPos = 0;
		}
	}

	@Override
	public GameMsg[] getLastMsg(int count) {
		if (count > MAX_RECORD_COUNT) {
			count = MAX_RECORD_COUNT;
		}

		int startIdx = currPos - count;
		if (startIdx < 0) {
			startIdx += MAX_RECORD_COUNT;
		}

		GameMsg[] result = new GameMsg[count];
		for (int i = 0; i < count; i++) {
			result[i] = recordMsgs[startIdx++];
			startIdx = (startIdx >= MAX_RECORD_COUNT) ? 0 : startIdx;
		}

		return result;
	}

	@Override
	public void clearMessage() {
		for (int i = 0; i < recordMsgs.length; i++) {
			recordMsgs[i] = null;
		}
		currPos = 0;
	}
}
